package com.linwen.javase.desgin_pattern.structure.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Composite
 * @Description
 * @Author lin
 * @Date 2022/6/21 下午10:58
 * @Version V1.0
 */
public class Composite implements Component {

    private String name;

    private List<Component> children = new ArrayList<Component>();

    public void add(Component component){
        children.add(component);
    }
    public void remove(Component component){
        children.remove(component);
    }
    public Component getChild(int index){
        return children.get(index);
    }

    public Composite() {
    }

    public Composite(String name) {
        this.name = name;
    }

    @Override
    public void operate() {
        for (Component component:children) {
            System.out.println("我是树枝： "+name);
            component.operate();
        }
    }
}
